import Vue from "vue";
import VueRouter from "vue-router";
// import AddGoods from "@/pages/AddGoods.vue";
// import GoodsList from "@/pages/GoodsList.vue";
// import MyCart from "@/pages/MyCart.vue";
// import My from "@/pages/My.vue";
// import Login from "@/pages/Login.vue";
// import Info from "@/pages/Info.vue";

// @/pages/AddGoods.vue 组件打包至名字one.js文件中
const AddGoods = ()=>import(/* webpackChunkName:"one" */"@/pages/AddGoods.vue");
// @/pages/GoodsList.vue 组件打包至名字goodsList.js文件中
const GoodsList = ()=>import(/* webpackChunkName:"goodsList" */"@/pages/GoodsList.vue");
// 如果名字相同，那么会将多个组件打包至同一个JS文件中
const MyCart = ()=>import(/* webpackChunkName:"one" */"@/pages/MyCart.vue");
const My = ()=>import(/* webpackChunkName:"one" */"@/pages/My.vue");
const Login = ()=>import(/* webpackChunkName:"one" */"@/pages/Login.vue");
const Info = ()=>import(/* webpackChunkName:"one" */"@/pages/Info.vue");
Vue.use(VueRouter);
const router = new VueRouter({
	routes:[
		{
			path:"/",
			component:AddGoods,
			meta:{
				isKeep:true
			}
		},{
			path:"/goodsList",
			component:GoodsList
		},{
			path:"/my",
			component:My,
			name:"my",
			meta:{
				isAuthorization:true
			},
			beforeEnter(to,from,next){
				if(localStorage.getItem("token")) next();
				else next({
					path:"/login",
					query:{
						returnUrl:"/my"
					}
				})
			},
			children:[
				{
					path:"/my",
					name:"myCart",
					component:MyCart
				},{
					path:"/my/info",
					component:Info
				}
			]

		},{
			path:"/login",
			meta:{
				hideNav:true
			},
			component:Login
		}
	],
	mode:"history"
});
// 全局前置守卫。
router.beforeEach(function(to,from,next){
	// 查看要去的路由是否需要验证身份权限
	if(to.meta.isAuthorization){
		if(localStorage.getItem("token")){
			next();
		}else{
			next({
				path:"/login",
				query:{
					returnUrl:to.fullPath
				}
			})
		}
	}else{
		next();
	}
})
export default router;